<?php


namespace App\Services\Tools;

//日期时间相关的方法
use phpDocumentor\Reflection\DocBlock\Description;

class DateTime
{
    /**
     * 获取本周或上周的日期范围（即周一到周日）
     * @param int $type 0：本周；-1：上周；-2：上上周
     * @return array
     */
    public static function getWeekDateRange($type = 0)
    {
        if ($type == 0) {
            $date = date('Y-m-d');
        } else {
            $date = date('Y-m-d', strtotime("$type week"));
        }

        $weekNo = date('N', strtotime($date));
        $frontDays = $weekNo - 1;
        $behindDays = 7 - $weekNo;

        $startDate = date('Y-m-d', strtotime("$date -$frontDays day"));
        $endDate = date('Y-m-d', strtotime("$date +$behindDays day"));
        return [$startDate, $endDate];
    }

    /**
     * 获取本月或上月的日期范围
     * @param int $type 0：本月；-1：上月；-2：上上月；
     * @return array
     */
    public static function getMonthDateRange($type = 0)
    {
        if ($type == 0) {
            $startDate = date('Y-m-01');
        } else {
            $mktime = mktime(0,0,0,date('n')+$type,1,date('Y'));
            $startDate = date('Y-m-01', $mktime);
        }
        $endDate = date('Y-m-d', strtotime("$startDate +1 month -1 day"));
        return [$startDate, $endDate];
    }

    //获取日期的时间戳范围
    public static function getDateTimestampRange($date)
    {
        return [strtotime($date . ' 00:00:00'), strtotime($date . ' 23:59:59')];
    }

    /**
     * 根据日期标签计算日期范围
     * @param int $dateTag 日期标签，-1：昨天；0：自定义日期范围；1：今天；3：最近3天；7：最近7天；30：最近30天
     * @param string $startDate 自定义开始日期
     * @param string $endDate 自定义接收日期
     * @return string|array
     */
    public static function getDateRangeByTag($dateTag, $startDate, $endDate)
    {
        if ($dateTag == -1) {
            $dateRange = date('Y-m-d', strtotime("-1 day"));
        } else if ($dateTag == 0) {
            $dateRange = [$startDate, $endDate];
        } else if ($dateTag == 1) {
            $dateRange = date('Y-m-d');
        } else if ($dateTag == 3) {
            $dateRange = [date('Y-m-d', strtotime("-2 day")), date('Y-m-d')];
        } else if ($dateTag == 7) {
            $dateRange = [date('Y-m-d', strtotime("-6 day")), date('Y-m-d')];
        } else if ($dateTag == 30) {
            $dateRange = [date('Y-m-d', strtotime("-30 day")), date('Y-m-d')];
        }
        return $dateRange;
    }
}